# -*- coding: utf-8 -*-
# @author: edgardeng
# @date:   2021-05-08
# @file:   
"""
 基于传输层TCP/IP协议接口socket实现的TCP发送json格式数据

 测试客户端-短连接
"""
#
from socket import *

if __name__ == '__main__':

    # 基础参数(这里填写要发送到的服务端地址端口)
    HOST = '127.0.0.1'  # or 'localhost'
    PORT = 21725
    BUFSIZ = 2048
    ADDR = (HOST, PORT)

    # 创建socket
    tcpCliSock = socket(AF_INET, SOCK_STREAM)
    # 试图连接到服务端
    tcpCliSock.connect(ADDR)

    # 用户控制台输入数据
    data_input = input('[client]: input message to send >')
    if not data_input:
        print('error')

    # 客户端发送给服务端
    tcpCliSock.send(data_input.encode())
    data_receive = tcpCliSock.recv(BUFSIZ)
    if not data_receive:
        print('error')
    print('[client]: {}'.format(data_receive.decode('utf-8')))

    # 一次短连接,三次握手结束,任何一方都可以发起close
    tcpCliSock.close()